<template>
  <!-- 
     部门选择：
         1：导入组织架构接口
         2：点击部门输入时调用
         3：调用成功储存并渲染
               树形数据转换
               通过el-tree渲染
               没有数据才调用接口
 -->
  <el-dialog title="新增员工" :visible="isShow" @close="cancelEvent">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" placeholder="新输入姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="请输入入职时间"
        />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <div v-clickOut="clickOutTrr">
          <el-input
            v-model="form.departmentName"
            placeholder="请输入部门"
            readonly
            @focus="focusEves"
          />
          <transition name="tree">
            <el-tree
              v-if="bol"
              style="min-height:200px"
              v-loading="loading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(0, 0, 0, 0.8)"
              :data="tretData"
              :props="{ label: 'name' }"
              @node-click="nodeClick"
            />
          </transition>
        </div>
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="请输入转正时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button type="primary" @click="subim">取消</el-button>
        <el-button @click="cancelEvent">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employees from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysuserPost } from '@/api/employees'
export default {
  props: {
    isShow: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      tretData: [],
      employees,
      bol: false,
      form: {
        username: '', // string	非必须		姓名
        mobile: '', //string	非必须		手机号
        formOfEmployment: '', //number	非必须		聘用形式
        workNumber: '', //string	非必须		工号
        departmentName: '', //string	非必须		组织名称
        timeOfEntry: '', //string	非必须		入职时间
        correctionTime: '' //string	非必须		转正时间
      },
      rules: {
        // 正则表达式
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号',
            trigger: 'change'
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    cancelEvent () {
      this.$emit('update:isShow', false)
      //  清空表单验证与数据
      this.$refs.form.resetFields()
      this.form = {
        username: '', // string	非必须		姓名
        mobile: '', //string	非必须		手机号
        formOfEmployment: '', //number	非必须		聘用形式
        workNumber: '', //string	非必须		工号
        departmentName: '', //string	非必须		组织名称
        timeOfEntry: '', //string	非必须		入职时间
        correctionTime: '' //string	非必须		转正时间
      }
    },
    async focusEves () {
      this.bol = true
      if (this.tretData.length === 0) {
        this.loading = true
        const res = await companyDepartment()
        this.tretData = this.chooseData(res.data.data.depts)
        this.loading = false
        console.log('获取部门信息', res)
      }
    },
    clickOutTrr () {
      this.bol = false
    },
    //  树形数据转换 1:找出第一层 2：找他的children(通过当前项的id)
    chooseData (arr, pid = '') {
      return arr.filter(item => {
        if (item.pid === pid) {
          item.children = this.chooseData(arr, item.id)
          return true
        }
      })
    },
    nodeClick (data) {
      this.form.departmentName = data.name
      //  关闭树形状态
      this.bol = false
    },
    // 确定点击
    async subim () {
      this.$refs.form.validate(async result => {
        if (result) {
          await sysuserPost(this.form)
          this.$message.success('新增成功')
          this.cancelEvent()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-enter {
  opacity: 0;
}
.tree-enter-active {
  transition: all 0.5s;
}
.tree-leave-to {
  opacity: 0;
}
.tree-leave-active {
  transition: all 0.5s;
}
</style>
